package uit.uthuoc.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import uit.uthuoc.dal.DrugDAO;
import uit.uthuoc.dal.DrugDTO;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.uit.uthuoc.R;

public class SearchResultFragment extends ListFragment {
	
	private static final String LOG_ID = "uit.uthuoc.fragment.SearchResultFragment";

    private SearchView mSearchView;
	private ArrayList<DrugDTO> mDrugList;
	private SearchResultFragmentInterface parentActivity;
	private ArrayAdapter<DrugDTO> adapter;
	private final OnQueryTextListener queryListener = new OnQueryTextListener() {

		@Override
		public boolean onQueryTextChange(String arg0) {
			return false;
		}

		@Override
		public boolean onQueryTextSubmit(String arg0) {
			onSearchButtonClicked(arg0);
			mSearchView.clearFocus();
			return false;
		}
		
	};
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);	
		mDrugList = DrugDAO.getDAO().getList();	
		adapter = new ArrayAdapter<DrugDTO>(getActivity(), R.layout.simple_list_item,(ArrayList<DrugDTO>) mDrugList.clone());
		setListAdapter(adapter);
	}
	
	

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		
	    // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            parentActivity = (SearchResultFragmentInterface) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement SearchResultFragmentInterface");
        }

	}

	
	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setHasOptionsMenu(true);
	}



	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		
        // Inflate the menu; this adds items to the action bar if it is present.
        inflater.inflate(R.menu.main_activity_actions, menu);
        
        MenuItem searchItem = menu.findItem(R.id.action_search);
        
        mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        mSearchView.setIconifiedByDefault(false);
        mSearchView.setQueryHint(getString(R.string.search_hint));
        mSearchView.setOnQueryTextListener(queryListener);
        
		super.onCreateOptionsMenu(menu, inflater);
	}
	
	public void onSearchButtonClicked(String searchText)
	{
		// Clear the current adapter
		adapter.clear();
		
		Random rand = new Random();
		int from = rand.nextInt(100);
		int to = rand.nextInt(100);
		
		if (from > to) {
			int temp = from;
			from = to;
			to = temp;
		}
		
		if (searchText.equals("all")) {
			from = 0;
			to = 100;
		}
		List<DrugDTO> newList = mDrugList.subList(from, to);
		
		for (DrugDTO drugDTO : newList) {
			adapter.add(drugDTO);
		}
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		parentActivity.onListItemClickListener(position);
	}
	
	
}
